// packageStudent/pages/detail/index.js
import {getSeniorDetail} from "../../../service/Personnel";
import {getRecruitmentList} from "../../../service/Position";
import {addFocus, delFocus} from "../../../service/Collection";
import {exchangeContact, sendGZHTemplate} from "../../../service/Message";
import Config from "../../../main.config";
import {getStorageSync} from "../../../utils/Storage";
import Dialog from "@vant/weapp/dialog/dialog";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageShow: false,
        detail: {},
        //招聘信息
        recruitment: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._id = options.id;
        this.data._registerInfo = getStorageSync(Config.storage.registerInfo) || {}
        this.getSeniorDetail();
    },

    /*
    *
    * 获取学长详情
    * */
    getSeniorDetail: async function () {
        try {
            let id = this.data._id;
            let res = await getSeniorDetail(id);
            let logoUrl = res.logo ? (Config.api.downLoad + "?fileId=" + res.logo) : null;
            this.setData({
                detail: {
                    id: res.id,
                    logo: logoUrl,
                    name: res.name,
                    grade: res.grade.split(";")[1].substr(0, 4),
                    schoolName: res.schoolName,
                    professionalName: res.professionalName,
                    position: res.position,
                    companyName: res.companyName,
                    companyId: res.companyId,
                    createUser: res.createUser,
                    focus: res.focus,
                    contact: res.contact,
                    eventId: res.eventId || 0,
                    status: res.status
                }
            })
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({
                pageShow: true,
                "recruitment.loading": true
            })
        }
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let companyId = this.data.detail.companyId;
            let paging = e.detail.paging;
            let list = this.data.recruitment.list;
            let isReset = this.data.recruitment.reset;
            let res = await getRecruitmentList({
                type: 1,
                companyId: companyId,
                page: paging.page,
                limit: paging.pageSize,
            });
            let newList = res.data;
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "recruitment.list": list,
                "recruitment.total": res.count
            });
        } catch (e) {
            this.setData({
                "recruitment.error": true
            })
        } finally {
            this.setData({
                "recruitment.reset": false,
                "recruitment.loading": false
            })
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"recruitment.loading": true})
    },

    /*
    * 跳转公司详情
    * */
    bindNavigatorToCompanyDetail: async function () {
        let companyId = this.data.detail.companyId;
        wx.navigateTo({
            url: "/pages/common/company-detail/index?from=student&companyId=" + companyId
        })
    },

    /*
    * 点击跳转详情
    * */
    bindNavigatorToRecruitmentDetail: function (e) {
        let id = e.currentTarget.dataset.id;
        let staffId = this.data.detail.id;
        wx.navigateTo({
            url: "/pages/common/recruitment-detail/index?from=student&id=" + id
            + "&staffId=" + staffId
        })
    },

    /*
    * 点击按钮
    * */
    bindClickButton: async function (e) {
        let detail = this.data.detail;
        let registerInfo = this.data._registerInfo;
        let id = detail.id;
        let toUserId = detail.createUser;
        let name = e.currentTarget.dataset.name;
        let loading = this.data["detail." + name + "Loading"];
        if (loading) return false; //防重复
        try {
            this.setData({["detail." + name + "Loading"]: true});
            switch (name) {
                case "gz": //关注
                    let focus = this.data.detail.focus;
                    focus < 1 ? await addFocus({
                        userId: registerInfo.id,
                        objectId: id
                    }) : await delFocus({
                        userId: registerInfo.id,
                        objectId: id
                    })
                    this.setData({"detail.focus": focus < 1 ? 1 : 0});
                    focus < 1 ? await sendGZHTemplate({
                        userId: toUserId,
                        pagepath: "/pages/index/index?active=1",
                        type: 1
                    }) : null
                    break
                case "jhlxfs": //交换联系方式
                    let contact = this.data.detail.contact;
                    if (contact > 0) {
                        await Dialog.alert({
                            title: "提示",
                            message: "交换联系方式每天只能申请一次"
                        })
                        return false
                    }
                    await exchangeContact({
                        messageUserid: id,
                        message: registerInfo.phone,
                        sendUserid: registerInfo.id
                    })
                    this.setData({
                        "detail.contact": 1,
                        "detail.eventId": 0
                    });
                    await Dialog.alert({
                        title: "提示",
                        message: "已经发送交互联系方式信息；请在【消息】栏目中查收"
                    })
                    await sendGZHTemplate({
                        userId: toUserId,
                        pagepath: "/pages/index/index?active=1",
                        type: 2
                    })
                    break
                default:
                    break
            }
        } catch (e) {
            console.error(e);
            await Dialog.alert({
                title: "提示",
                message: e.code === 550 ? e.message : "发送失败！请返回上一页刷新页面重新尝试"
            })
        } finally {
            this.setData({["detail." + name + "Loading"]: false});
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})